/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package IRS.Main;

import IRS.Domains.LocalResult;
import IRS.Domains.Query;
import IRS.Logic.RocchioAlgorithm;
import com.google.code.bing.search.client.BingSearchClient;
import com.google.code.bing.search.client.BingSearchClient.SearchRequestBuilder;
import com.google.code.bing.search.client.BingSearchServiceClientFactory;
import com.google.code.bing.search.schema.AdultOption;
import com.google.code.bing.search.schema.SearchOption;
import com.google.code.bing.search.schema.SearchResponse;
import com.google.code.bing.search.schema.SourceType;
import com.google.code.bing.search.schema.web.WebResult;
import com.google.code.bing.search.schema.web.WebSearchOption;
import java.util.ArrayList;
import java.util.List;
import java.io.*;
import java.util.Scanner;

/**
 *
 * @author lin
 */
public class Main {

    public static SearchResponse search(Query query) {
        BingSearchServiceClientFactory factory = BingSearchServiceClientFactory.newInstance();
        BingSearchClient client = factory.createBingSearchClient();

        SearchRequestBuilder builder = client.newSearchRequestBuilder();
        builder.withAppId(applicationId);
        builder.withQuery(query.getQuery());
        builder.withSourceType(SourceType.WEB);
        builder.withVersion("2.0");
        builder.withMarket("en-us");
        builder.withAdultOption(AdultOption.MODERATE);
        builder.withSearchOption(SearchOption.ENABLE_HIGHLIGHTING);

        builder.withWebRequestCount(10L);
        builder.withWebRequestOffset(0L);
        SearchRequestBuilder withWebRequestSearchOption = builder.withWebRequestSearchOption(WebSearchOption.DISABLE_HOST_COLLAPSING);
        builder.withWebRequestSearchOption(WebSearchOption.DISABLE_QUERY_ALTERATIONS);

        return client.search(builder.getResult());
    }
    
    
    private static String applicationId = "A0F9D1C1BFB55779A0B04C1BADA55A992BFB096B";

    public static void main(String[] args) {

        double precision = 0.1;
		double preset_precision = 0;

		BufferedReader reader;
		reader = new BufferedReader(new InputStreamReader(System.in));
		String input_query = "";

		System.out.println("Please input equery: ");
		try{
			input_query = reader.readLine();
		}catch(IOException ioe){
			System.out.println("Error Input");
		}
		Query query = new Query(input_query);


		System.out.println("Please input precision value: ");
		try{
			preset_precision = Double.parseDouble(reader.readLine());
		}catch(IOException ioe){
			System.out.println("Error Input");
		}

		System.out.println("Parameters:");
		System.out.println("Chlient Key = "+applicationId);
		System.out.println("Query = "+ input_query);
		System.out.println("Precision = "+ preset_precision);
		System.out.println("URL: http://api.bing.net/xml.aspx?AppId=" +applicationId + "&Query="+ input_query.replace(' ', '+')+"&Source=Web");
		System.out.println("Total no of results : 10");
		System.out.println("Bing Search Results:");
		System.out.println("========================");

        while (true) {

            int relevant_count = 0;
            int nonrelevant_count = 0;
			int count = 1;

            SearchResponse response = search(query);

            InputStream is = System.in;
            Scanner scan = new Scanner(is);
            List<LocalResult> results = new ArrayList<LocalResult>();

            for (WebResult result : response.getWeb().getResults()) {

				System.out.println("\nResult "+ count );
				System.out.println("[");
                System.out.println(result.getTitle());
                System.out.println(result.getDescription());
                System.out.println(result.getUrl());
                System.out.println(result.getDateTime());
				System.out.println("]");

                boolean relevant = false;
                System.out.print("\nRelevant (Y/N)?");
                String input = scan.next();
                if (input.equalsIgnoreCase("n")) {
                    relevant = false;
                    nonrelevant_count++;
                } else {
                    relevant = true;
                    relevant_count++;
                }
                results.add(new LocalResult(result, relevant));
				count++;
            }
			
			precision = (double)relevant_count / (double)(relevant_count+nonrelevant_count);

			System.out.println("=============================");
	        System.out.println("Feedback Summary:");
			System.out.println("Query: " + query.getKeywords().toString());
			System.out.println("Precision: "+ precision);
			if (precision >= preset_precision){
				System.out.println("Desired precision reached, done");
				break;
			}else if(precision == 0){
	        	System.out.println("Still below the desired precision of"+preset_precision);
				System.out.println("Indexing results ....");
				System.out.println("Indexing results ....");
				System.out.println("Argumenting by");
				System.out.println("Below desired precision, but can no longer augment the query");
				break;
			}else{
				System.out.println("Still below the desired precision of"+preset_precision);
				System.out.println("Indexing results ....");
				System.out.println("Indexing results ....");
				System.out.println("Augumenting by native south");
			}

			

            RocchioAlgorithm ra = new RocchioAlgorithm(query, results);
            query = ra.execute();
        }
    }
}
